/**
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-04-18 23:54:05
 * Copyright (c) 2015- -版权所有
 */

package com.joysuch.wwyt.core.controller;

import cn.zlg.common.springmvc.validate.constraints.NotNull;
import com.joysuch.wwyt.core.bean.BaseMenuFunctionEditBean;
import com.joysuch.wwyt.core.bean.BaseMenuFunctionListBean;
import com.joysuch.wwyt.core.bean.BaseMenuFunctionListBean.SearchBaseMenuFunction;
import com.joysuch.wwyt.core.bean.BaseMenuFunctionStateBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.entity.BaseMenuFunction;
import com.joysuch.wwyt.core.service.BaseMenuFunctionService;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.core.support.Context;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/base/menu-function")
@Api(tags = "/base/menu-function 菜单")
public class BaseMenuFunctionController {

    private static final Logger logger = LoggerFactory.getLogger(BaseMenuFunctionController.class);

    @Autowired
    private BaseMenuFunctionService baseMenuFunctionService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("base:menu:function:saveState")
    @PostMapping("saveState")
    @ApiOperation("设置菜单显示状态")
    public ResultBean saveState(@RequestBody BaseMenuFunctionStateBean bean) {
        BaseMenuFunction function = baseMenuFunctionService.saveState(bean);
        return ResultBean.success(function);
    }

    @RequiresPermissions("base:menu:function:add")
    @RequestMapping("add")
    @ApiOperation("增加菜单")
    public Object add(@Valid @RequestBody BaseMenuFunctionEditBean entity) {
        baseMenuFunctionService.add(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("base:menu:function:update")
    @RequestMapping("update")
    @ApiOperation("修改菜单")
    public Object update(@Valid @RequestBody BaseMenuFunctionEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        baseMenuFunctionService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("base:menu:function")
    @RequestMapping("page")
    @ApiOperation("分页显示菜单")
    public Object page(Pageable page, SearchBaseMenuFunction condition) {
        Page<BaseMenuFunctionListBean> data = baseMenuFunctionService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("base:menu:function")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchBaseMenuFunction condition) {
        Page<BaseMenuFunctionListBean> data = baseMenuFunctionService.findByPage(page, condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(BaseMenuFunction.class, "name");
        return ResultBean.success(data);
    }

    @RequestMapping("parent-list")
    public Object parentList(String type, Long parentId) {
        List<SimpleSelectListBean> data = baseMenuFunctionService.parentList(type, parentId, Context.getCurrentTenantId(), Context.getCurrentOrgCode());
        return ResultBean.success(data);
    }

    @RequiresPermissions("base:menu:function:delete")
    @RequestMapping("delete")
    public Object delete(Long id) {
        baseMenuFunctionService.delete(id);
        return ResultBean.success("");
    }

    @RequiresPermissions("base:menu:function")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field = "id") Long id) {
        return ResultBean.success(baseMenuFunctionService.getById(id));
    }

    //获取顶级菜单 depth=1
    @GetMapping("getTopFun")
    public ResultBean getTopFun() {
        return baseMenuFunctionService.getTopFun();
    }
}
